package com.data.utill;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by cxj on 2017/5/31.
 *  md5加密算法用于对密码进行加密。使用方法：
 *  MD5Encrypt.encrypt(String)
 *  return 加密好的字符串
 */
public class MD5Encrypt {

    public static byte[] MD5Encode(byte[] bytes){
        MessageDigest md=null;
        try {
            md = MessageDigest.getInstance("MD5");
            return md.digest(bytes);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return new byte[0];
        }
    }

    public static final String bytesToHexString(byte[] bArray){
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }
    /*对字符串进行加密*/
    public static String encrypt(String inputStr){
        return bytesToHexString(MD5Encrypt.MD5Encode(inputStr.getBytes()));
    }

}
